package org.springblade.resource.controller;

import lombok.AllArgsConstructor;
import org.springblade.resource.builder.oss.OssBuilder;
import org.springblade.resource.ueditor.ActionEnter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@RestController
@AllArgsConstructor
public class UEditorController {

	/**
	 * 对象存储构建类
	 */
	private final OssBuilder ossBuilder;

	@RequestMapping("/ueditor/config")
	public void config(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("application/json");
		String rootPath = request.getSession().getServletContext().getRealPath("/");
		try {
			String exec = new ActionEnter(request, rootPath, ossBuilder).exec();
			PrintWriter writer = response.getWriter();
			writer.write(exec);
			writer.flush();
			writer.close();
			response.setHeader("X-Frame-Options", "SAMEORIGIN");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
